#include "memory.h"
#include <bits/stdc++.h>

int Memory(int N, int M) {
	if(N & 1) return -2;
	int pos = M & 127, cnt = (M >> 7) & 127, dep = (M >> 14) & 127, lst = M >> 21 & 1;
//	if(pos >= N) return 0;
	std :: cerr << M << " " << lst << " " << pos << " " << cnt << " " << dep << std :: endl;
	char ch = Get(++pos);
	if(ch == '<') {
		++cnt; if(cnt == dep) lst = 0;
	} else if(ch == '>') {
		if(cnt == 0) return -2;
		if(cnt == dep && lst != 0) return -2;
		cnt--;
	} else if(ch == '[') {
		++cnt; if(cnt == dep) lst = 1;
	} else {
		if(cnt == 0) return -2;
		if(cnt == dep && lst != 1) return -2;
		cnt--;
	}
	if(pos == N) {
		if(cnt) return -2;
		if(dep == (N >> 1)) return -1;
		pos = 0; dep++;
	}
	std :: cerr << (lst | (pos << 1) | (cnt << 8) | (dep << 15)) << std :: endl;
	return (pos | (cnt << 7) | (dep << 14) | (lst << 21));
}
